﻿// -----------------------------------------------------------------------
// <copyright file="MetalContextMenu.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using Metal.Config;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MetalContextMenu : ContextMenuStrip
    {
        public static Color menuForeColor;
        public static Color dropDownMenuForeColor;
        public static Color dropDownMenuBackColor;
        public static Color imageMarginGradientBegin;
        public static Color imageMarginGradientMiddle;
        public static Color imageMarginGradientEnd;

        public static Color menuItemSelected;
        public static Color menuItemSelectedGradientBegin;
        public static Color menuItemSelectedGradientEnd;
        public static Color menuItemPressedGradientBegin;
        public static Color menuItemPressedGradientEnd;
        public static Color menuBorder;
        public static Color menuItemBorder;

        /// <summary> Our font object </summary>
        private System.Drawing.Text.PrivateFontCollection pfc;

        public MetalContextMenu()
        {
            this.RenderMode = ToolStripRenderMode.ManagerRenderMode;
            this.Renderer = new MyRenderer();
        }

        public void SetFont(string fontName, int fontSize, System.Drawing.FontStyle fontStyle)
        {
            if (fontName == "Default")
            {
                return;
            }

            // Load font from file
            this.pfc = new System.Drawing.Text.PrivateFontCollection();

            this.pfc.AddFontFile(Metal.Config.Paths.ThemesFolder + Settings.Instance.GetValue("LastSettings.Theme", "BlackMetal") + "\\" + fontName + ".ttf");

            // Apply font
            this.Font = new System.Drawing.Font(this.pfc.Families[0], fontSize, fontStyle);
        }

        private class MyRenderer : ToolStripProfessionalRenderer
        {
            public MyRenderer() : base(new MyColors()) { }

            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (e.Item.IsOnDropDown)
                {
                    e.ToolStrip.BackColor = dropDownMenuBackColor;
                }
                else
                {
                    e.Item.BackColor = Color.Transparent;
                }
                base.OnRenderMenuItemBackground(e);
            }

            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                if (e.Item.IsOnDropDown)
                {
                    e.TextColor = dropDownMenuForeColor;
                }
                else
                {
                    e.TextColor = menuForeColor;
                }

                base.OnRenderItemText(e);
            }
        }

        private class MyColors : ProfessionalColorTable
        {
            public override Color ImageMarginGradientBegin
            {
                get
                {
                    return imageMarginGradientBegin;
                }
            }

            public override Color ImageMarginGradientMiddle
            {
                get
                {
                    return imageMarginGradientMiddle;
                }
            }

            public override Color ImageMarginGradientEnd
            {
                get
                {
                    return imageMarginGradientEnd;
                }
            }

            public override Color MenuItemBorder
            {
                get
                {
                    return menuItemBorder;
                }
            }

            public override Color MenuBorder
            {
                get
                {
                    return menuBorder;
                }
            }

            public override Color ToolStripDropDownBackground
            {
                get
                {
                    return dropDownMenuBackColor;
                }
            }

            public override Color MenuItemPressedGradientBegin
            {
                get
                {
                    return menuItemPressedGradientBegin;
                }
            }

            public override Color MenuItemPressedGradientEnd
            {
                get
                {
                    return menuItemPressedGradientEnd;
                }
            }

            public override Color MenuItemSelected
            {
                get
                {
                    return menuItemSelected;
                }
            }

            public override Color MenuItemSelectedGradientBegin
            {
                get
                {
                    return menuItemSelectedGradientBegin;
                }
            }

            public override Color MenuItemSelectedGradientEnd
            {
                get
                {
                    return menuItemSelectedGradientEnd;
                }
            }
        }
    }
}
